package me.nibo.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * @author nibo
 */
public class MySimpleAdapter extends RecyclerView.Adapter<MySimpleAdapter.MyViewHolder> {
    private static final String TAG = MySimpleAdapter.class.getSimpleName();
    private Context mContext;
    protected List<String> mDatas;
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;

    public MySimpleAdapter(Context context, List<String> datas) {
        this.mContext = context;
        this.mDatas = datas;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_single_textview, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.tv.setText(mDatas.get(position));
        if (null != mOnItemClickListener) {
            setUpItemEvent(holder);
        }
    }

    protected void setUpItemEvent(final MyViewHolder holder) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 通过holder获取到position，避免当新添加item时
                // 点击的item元素索引不正确
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onClickItem(holder.itemView, pos);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onLongClickItem(holder.itemView, pos);
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 添加数据
     * @param pos 索引
     */
    public void add(int pos) {
        mDatas.add(pos, "Insert one");
        notifyItemInserted(pos);
    }

    /**
     * 删除数据
     * @param pos 索引
     */
    public void delete(int pos) {
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }

    /**
     * item元素点击事件接口
     */
    public interface OnItemClickListener {
        /**
         * 点击item元素事件
         * @param view 当前点击元素的view
         * @param position 当前点击元素的索引
         */
        void onClickItem(View view, int position);

        /**
         * 长按item元素事件
         * @param view 当前元素的view
         * @param position 当前元素的索引
         */
        void onLongClickItem(View view, int position);
    }
}


